package cn.edu.njupt.eclass.aixinling.controller;

import cn.edu.njupt.eclass.aixinling.domain.BaseResponse;
import cn.edu.njupt.eclass.aixinling.model.Tag;
import cn.edu.njupt.eclass.aixinling.service.TagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: kent
 * @date: 2018/6/4 20:48
 */
@RestController
@Slf4j
@RequestMapping(value = "/tags")
@CrossOrigin
public class TagController {

    @Autowired
    TagService tagService;


    @RequestMapping(value = "/list.do",method = RequestMethod.GET)
    @ResponseBody
    private BaseResponse getList(@RequestParam(value = "num",required = false) Integer num){
        log.info("请求List.do接口");
        if(num != null){
            log.info("返回指定数目Tag");
            return BaseResponse.success(tagService.getTagsByNum(num));
        }
        log.info("获取一条数据");
        return BaseResponse.success(tagService.getHighestWeightTag());
    }

    @RequestMapping(value = "/all.do",method = RequestMethod.GET)
    @ResponseBody
    private BaseResponse getAll(){
        log.info("请求所有的Tag");
        return BaseResponse.success(tagService.getAll());
    }

    @RequestMapping(value = "/add.do" ,method = RequestMethod.POST)
    @ResponseBody

    private BaseResponse addNewTag(@RequestParam("title") String title,
                                   @RequestParam("pattern") Integer picturePattern,
                                   @RequestParam("url") String externalUrl,
                                   @RequestParam("abstract") String tagAbstract,
                                   @RequestParam("pId") Integer pictureId,
                                   @RequestParam(value = "pUrl" ,required = false)  String pictureUrl){
        log.info("新增加一个Tag");
        Tag tag = new Tag();
        tag.setTitle(title);
        tag.setPicturePattern(picturePattern);
        tag.setExternalUrl(externalUrl);
        tag.setTagAbstract(tagAbstract);
        tag.setWeight(tagService.getHighestWeightTag().getWeight() - 1);//给第二权重
        tag.setPictureId(pictureId);
        tag.setPictureUrl(pictureUrl);
        tag.setStatus(0);//默认审核
        int r = tagService.addNewTag(tag);
        System.out.println("返回的int 类型的 r = "+ r );
        return BaseResponse.success();
    }

    @RequestMapping(value = "/delete.do" ,method = RequestMethod.GET)
    @ResponseBody
    private BaseResponse deleteTagById(@RequestParam("id") Integer id){
        log.info("删除指定id标签");
        if (tagService.getTagById(id) != null){
            tagService.deleteById(id);
            return BaseResponse.success("删除成功");
        }
        return BaseResponse.badrequest("标签不存在");

    }


    @RequestMapping(value = "/remove.do" ,method = RequestMethod.GET)
    @ResponseBody
    private BaseResponse unpublishTag(@RequestParam("id") Integer id){
        log.info("移除id标签");
        if (tagService.getTagById(id) != null){
            tagService.setTagUnpublishedById(id);
            return BaseResponse.success("移除成功");
        }
        return BaseResponse.badrequest("标签不存在");
    }


    @RequestMapping(value = "/publish.do" ,method = RequestMethod.GET)
    @ResponseBody
    private BaseResponse publishTag(@RequestParam("id") Integer id){
        log.info("发布id标签");
        if (tagService.getTagById(id) != null){
            tagService.setTagPublishedById(id);
            return BaseResponse.success("发布成功");
        }
        return BaseResponse.badrequest("标签不存在");
    }


    @RequestMapping(value = "/setTop.do" ,method = RequestMethod.GET)
    @ResponseBody
    private BaseResponse setTop(@RequestParam("id") Integer id){
        log.info("置顶id标签");
        if (tagService.getTagById(id) != null){
            tagService.setTagsTopById(id);
            return BaseResponse.success("置顶成功");
        }
        return BaseResponse.badrequest("标签不存在");

    }


    @RequestMapping(value = "/edit.do" ,method = RequestMethod.POST)
    @ResponseBody

    private BaseResponse edit(@RequestParam("id") Integer id,
                              @RequestParam("title") String title,
                              @RequestParam("pattern") Integer picturePattern,
                              @RequestParam("url") String externalUrl,
                              @RequestParam("abstract") String tagAbstract,
                              @RequestParam("pId") Integer pictureId,
                              @RequestParam(value = "pUrl" ,required = false)  String pictureUrl){
        log.info("编辑id标签");
        Tag tag = new Tag();
        tag.setId(id);
        tag.setTitle(title);
        tag.setPicturePattern(picturePattern);
        tag.setExternalUrl(externalUrl);
        tag.setTagAbstract(tagAbstract);
        tag.setWeight(tagService.getTagById(id).getWeight());//设置权重
        tag.setPictureId(pictureId);
        tag.setPictureUrl(pictureUrl);
        tag.setStatus(0);//编辑完成，默认为审核，需要手动发布
        if (tagService.getTagById(id) != null){
            tagService.EditTagById(tag);
            return BaseResponse.success("更新成功");
        }
        return BaseResponse.badrequest("标签不存在");
    }
}
